{{- $tfModel := ""}}
{{- $tfDao := ""}}
{{- range .tplFiles}}
{{- if eq .Type "Model"}}{{$tfModel = .}}{{- end}}
{{- if eq .Type "Dao"}}{{$tfDao = .}}{{- end}}
{{- end}}

{{- $model := toCamel .table.Name}}{{- /*Mode名字*/}}
{{- $dao := printf "%sDao" $model }}{{- /*Dao*/}}
{{- .ctx.Set "fileName" $model}}{{- /*设置输出文件名字*/}}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="{{$tfDao.Package}}.{{$dao}}">

    <!--  start  自动生成代码快，禁止手动修改         -->

    <sql id="columns">
        {{range $idx,$val := .table.Columns}}
        {{- if eq $idx 0}}{{.Name}}{{- else}}, {{.Name}}{{end}}
        {{- if eq $idx 10}}
        {{""}}
        {{- end}}
        {{- $mod := mod $idx 10}}
        {{- if and (gt $idx 10) (eq $mod 0)}}
        {{""}}
        {{- end}}
        {{- end}}
    </sql>

    <sql id="where">
        <trim prefix="WHERE" prefixOverrides="AND|OR">
            {{- range .table.Columns}}
            {{- $lowColName := toCamelLower .Name}}
            {{if eq .LangDataType "String"}}<if test="{{$lowColName}} !=null and '' != {{$lowColName}}"> AND a.{{.Name}} = #{{"{"}}{{$lowColName}},jdbcType={{mapGet $.jdbcVars .DataType}}{{"}"}}</if>
            {{- else}}<if test="{{$lowColName}} !=null"> AND a.{{.Name}} = #{{"{"}}{{$lowColName}},jdbcType={{mapGet $.jdbcVars .DataType}}{{"}"}}</if>
            {{- end}}
            {{- end}}
        </trim>
    </sql>

    <sql id="set">
        <trim prefix="SET" suffixOverrides=",">
            {{- range .table.Columns}}
            {{- $lowColName := toCamelLower .Name}}
            <if test="{{$lowColName}} !=null"> {{.Name}} = #{{"{"}}{{$lowColName}},jdbcType={{mapGet $.jdbcVars .DataType}}{{"}"}},</if>
            {{- end}}
        </trim>
    </sql>

    <sql id="order">
        <if test="orderClause != null and '' != orderClause"> ORDER BY ${orderClause} </if>
    </sql>

    <select id="findById" resultType="{{$tfModel.Package}}.{{$model}}" parameterType="String">
        SELECT <include refid="columns" /> FROM {{.table.Name}} a  WHERE a.id = #{id}
    </select>

    <select id="findByBean" resultType="{{$tfModel.Package}}.{{$model}}" parameterType="{{$tfModel.Package}}.{{$model}}">
        SELECT <include refid="columns" /> FROM {{.table.Name}} a
        <include refid="where" />
        <include refid="order" />
    </select>

    <select id="findByBeanCount" resultType="integer" parameterType="{{$tfModel.Package}}.{{$model}}">
        SELECT COUNT(1) FROM {{.table.Name}} a
        <include refid="where" />
    </select>

    <delete id="deleteById" parameterType="{{$tfModel.Package}}.{{$model}}">
        DELETE FROM {{.table.Name}} WHERE id = #{id}
    </delete>

    <delete id="deleteByBean" parameterType="{{$tfModel.Package}}.{{$model}}" >
        DELETE a FROM {{.table.Name}} a
        <include refid="where"/>
        and 1 = 1 <!-- 如果是一个空对象，那么 deleteByBean 会报错，强制要求至少要传一个条件-->
    </delete>

    <insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="{{$tfModel.Package}}.{{$model}}">
        INSERT INTO {{.table.Name}} (
            {{range $idx,$val := .table.Columns}}
            {{- if eq $idx 0}}{{.Name}}{{- else}}, {{.Name}}{{end}}
            {{- if eq $idx 10}}
            {{""}}
            {{- end}}
            {{- $mod := mod $idx 10}}
            {{- if and (gt $idx 10) (eq $mod 0)}}
            {{""}}
            {{- end}}
            {{- end}}
        )
        VALUES (
            {{range $idx,$val := .table.Columns}}
            {{- $lowColName := toCamelLower .Name}}
            {{- if eq $idx 0}}#{{"{"}}{{$lowColName}},jdbcType={{mapGet $.jdbcVars .DataType}}{{"}"}}{{- else}}, #{{- "{"}}{{$lowColName}},jdbcType={{mapGet $.jdbcVars .DataType}}{{"}"}}{{- end}}
            {{- if eq $idx 4}}
            {{""}}
            {{- end}}
            {{- $mod := mod $idx 4}}
            {{- if and (gt $idx 4) (eq $mod 0)}}
            {{""}}
            {{- end}}
            {{- end}}
        )
    </insert>

    <insert id="insertSelective" useGeneratedKeys="true" keyProperty="id" parameterType="{{$tfModel.Package}}.{{$model}}">
        INSERT INTO {{.table.Name}} (
        <trim prefix="" suffixOverrides=",">
            {{- range $idx,$val := .table.Columns}}
            {{- $lowColName := toCamelLower .Name}}
            <if test="{{$lowColName}} !=null"> {{$lowColName}},</if>
            {{- end}}
        </trim>
        )
        VALUES (
        <trim prefix="" suffixOverrides=",">
            {{- range $idx,$val := .table.Columns}}
            {{- $lowColName := toCamelLower .Name}}
            <if test="{{$lowColName}} !=null"> #{{"{"}}{{$lowColName}},jdbcType={{mapGet $.jdbcVars .DataType}}{{"}"}},</if>
            {{- end}}
        </trim>
        )
    </insert>

    <update id="updateById" parameterType="{{$tfModel.Package}}.{{$model}}">
        {{- $idCol := ""}}
        {{- range .table.Columns}}
        {{- if .IsPrimaryKey}}{{- $idCol = .}}{{- end}}
        {{- end}}
        UPDATE {{.table.Name}}
        <include refid="set"/>
        WHERE {{$idCol.Name}} = #{{"{"}}{{toCamelLower $idCol.Name}},jdbcType={{mapGet $.jdbcVars $idCol.DataType}}{{"}"}}
    </update>

    <!--  end  自动生成代码快，禁止手动修改        -->

</mapper>
